<?php
namespace App\Traits;

use App\Services\Api\Api;
use Cache;
trait CacheTrait
{
    public function cacheCeilData($ceilApiId, $ceilCode)
    {
        // if (Cache::has('cache_' . $ceilCode)) {
        //     return Cache::get('cache_' . $ceilCode);
        // }
        $obj = new Api();
        $result = $obj->getJavaCeilData($ceilApiId, $ceilCode);
        if ($result && count(@$result['elements']) > 0) {
            Cache::put('cache_' . $ceilCode, $result['elements'], 100);
            Cache::forever('cache_' . $ceilCode . '_forever', $result['elements']);
            return $result['elements'];
        }
        if (Cache::has('cache_' . $ceilCode . '_forever')) {
            return Cache::get('cache_' . $ceilCode . '_forever');
        } else {
            return [];
        }
    }

    /**
     * 返回市场合作 需求列表缓存数据
     * @param  [type] $result [description]
     * @return [type]         [description]
     */
    public function cacheGetMarketNeedsList($result)
    {
        if ($result && count(@$result['model_list']) > 0) {
            Cache::put('market_needs_ceil_data', $result['model_list'], 100);
            Cache::forever('market_needs_ceil_data_forever', $result['model_list']);
            return $result['model_list'];
        }
        if (Cache::has('market_needs_ceil_data_forever')) {
            return Cache::get('market_needs_ceil_data_forever');
        } else {
            return [];
        }
    }

    /**
     * 返回市场合作 项目列表缓存数据
     * @param  [type] $result [description]
     * @return [type]         [description]
     */
    public function cacheGetMarketProjectAdList($result)
    {
        if ($result && count(@$result['dataList']) > 0) {
            Cache::put('market_project_ceil_data', $result['dataList'], 100);
            Cache::forever('market_project_ceil_data_forever', $result['dataList']);
            return $result['dataList'];
        }
        if (Cache::has('market_project_ceil_data_forever')) {
            return Cache::get('market_project_ceil_data_forever');
        } else {
            return [];
        }
    }

    public function cacheGetIndustrySolutionData($result, $type)
    {
        if ($result && count(@$result['dataList']) > 0) {
            Cache::forever('industry_index_solution_data' . $type . '_forever', $result['dataList']);
            return $result['dataList'];
        }
        if (Cache::has('industry_index_solution_data' . $type . '_forever')) {
            return Cache::get('industry_index_solution_data' . $type . '_forever');
        } else {
            return [];
        }
    }

    public function cacheGetIndustryGreenResultData($result)
    {
        if ($result && count(@$result['dataList']) > 0) {
            Cache::forever('industry_index_green_result_data_forever', $result['dataList']);
            return $result['dataList'];
        }
        if (Cache::has('industry_index_green_result_data_forever')) {
            return Cache::get('industry_index_green_result_data_forever');
        } else {
            return [];
        }
    }

    /**
     * 在缓存中根据domainid获取子集列表
     * @param  [type] $domainId [description]
     * @return [type]           [description]
     */
    public function cacheGetDomainChildrenList($domainId)
    {
        $cacheInfo = Cache::get('other_dic_from_parent_id_get_children_list');
        $domainCacheInfo = $cacheInfo['LYZD'];
        return isset($domainCacheInfo[$domainId]) ? $domainCacheInfo[$domainId] : false;
    }

    public function cacheGetOtherDicList()
    {
        return Cache::get('other_dic_from_parent_id_get_children_list');
    }

    public function cacheGetOtherDicNameList()
    {
        return Cache::get('other_dic_from_id_get_name');
    }

    /**
     * 返回省列表
     * @param  [type] $areaId [description]
     * @return [type]         [description]
     */
    public function cacheGetProvinceList($areaId = 1)
    {
        $cacheInfo = Cache::get('area_from_parent_id_get_children_list');
        return $cacheInfo[$areaId];
    }

    public function cacheGetAreaList()
    {
        return Cache::get('area_from_parent_id_get_children_list');
    }

    public function getAreaNameList()
    {
        return Cache::get('area_from_id_get_name');
    }

    public function cacheFromAreaIdGetName($areaId)
    {
        $cacheInfo = Cache::get('area_from_id_get_name');
        return $cacheInfo[$areaId]['name'];
    }

    public function cacheDicList($apiCode)
    {
        $obj = new Api();
        $postData = $obj->getJavaPostData($apiCode, []);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            $this->_cacheDicInfo($result);
        }
    }

    public function cacheAreaList($apiCode)
    {
        $obj = new Api();
        $postData = $obj->getJavaPostData($apiCode, []);
        $result = $obj->getJavaResponseData($postData);

        if ($result) {
            $this->_cacheAreaInfo($result);
        }
    }

    /**
     * 缓存字典信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    private function _cacheDicInfo($data)
    {
        //根据ID获取名称
        $fromIdGetName = [];
        foreach ($data as $key => $value) {
            foreach ($value['conventionRows'] as $key1 => $value1) {
                $fromIdGetName[$value1['id']] = [
                    'name' => $value1['name'],
                ];
            }
        }

        //根据ID获取子集
        $fromParentIdGetChildrenList1 = [];
        foreach ($data as $key => $value) {
            $bookId = $value['book']['no'];
            $fromParentIdGetChildrenList1[$bookId] = [];

            foreach ($value['conventionRows'] as $key1 => $value1) {
                $fromParentIdGetChildrenList1[$bookId][] = [
                    'id' => $value1['id'],
                    'pid' => $value1['pid'],
                    'name' => $value1['name'],
                    'domain' => $value1['domain'],
                ];
            }
        }
        $fromParentIdGetChildrenList = [];
        foreach ($fromParentIdGetChildrenList1 as $key => $value) {
            foreach ($value as $key1 => $value1) {
                $fromParentIdGetChildrenList[$key][$value1['pid']][] = [
                    'id' => $value1['id'],
                    'name' => $value1['name'],
                    'domain' => $value1['domain'],
                ];
            }

        }

        Cache::forever('other_dic_from_parent_id_get_children_list', $fromParentIdGetChildrenList);
        Cache::forever('other_dic_from_id_get_name', $fromIdGetName);
    }

    /**
     * 缓存地区字典
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    private function _cacheAreaInfo($data)
    {
        $fromIdGetName = [];
        foreach ($data as $key => $value) {
            $fromIdGetName[$value['id']] = [
                'name' => $value['name'],
                'parent_id' => $value['pid'],
            ];
        }

        $fromParentIdGetChildrenList = [];
        foreach ($data as $key => $value) {
            $fromParentIdGetChildrenList[$value['pid']][] = [
                'id' => $value['id'],
                'name' => $value['name'],
            ];
        }
        Cache::forever('area_from_parent_id_get_children_list', $fromParentIdGetChildrenList);
        Cache::forever('area_from_id_get_name', $fromIdGetName);
    }

}
